package com.zsummer.zero.thread;

/**
 * wait() notify() notifyAll() 
 * wait() = wait(0)
 * wait����������*��ǰ�߳̽���ȴ�״̬* ͬʱ�ͷ��������е���
 * notify�� notifyAll�ǻ��ѵ�ǰ�߳��ϵĵȴ��߳�
 * ��notify����ʱ�������ʱ�򣬹���waitʱ����Զ�ȥ��ȡ��Ȼ������ִ��
 * @author zhouyujiang
 *
 */
public class ThreadDemo3 {
	
	private String[] shared = {"A"};
	
	public static void main(String[] args) {
		ThreadDemo3 threadDemo3 = new ThreadDemo3();
		WaitThread waitThread1 = threadDemo3.new WaitThread();
		WaitThread waitThread2 = threadDemo3.new WaitThread();
		WaitThread waitThread3 = threadDemo3.new WaitThread();
		NotifyThread notifyThread = threadDemo3.new NotifyThread();
		
		waitThread1.start();
		waitThread2.start();
		waitThread3.start();
		notifyThread.start();
		
	}
	
	class WaitThread extends Thread {
		@Override
		public void run() {
			synchronized (shared) {
				while("A".equals(shared[0])) {
					System.out.println(Thread.currentThread().getName()+" wait");
					try {
						shared.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+" notifyed!");
				}
			}
		}
	}
	
	class NotifyThread extends Thread {
		@Override
		public void run() {
			try {
				sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (shared) {
				System.out.println(Thread.currentThread().getName()+" notifyAll");
				shared[0] = "";
				shared.notifyAll();
			}
		}
	}
}
